#region Usings

using System;
using System.IO;
using NUnit.Framework;
using Karma.Framework.Core.Domain.Tools.Import.Data;
using Karma.Framework.Core.Domain.Tools.Import.Layout;
using Karma.Framework.Core.Test.Domain.Tools.Importador.Entidades;

#endregion

namespace Karma.Framework.Core.Test.Domain.Tools.Importador
{
    [TestFixture]
    public class EntityBuilderFixture
    {
        [Test]
        public void BuildEntidadConAsociadosConIdRefTest()
        {
            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadConAsociadosConIdRef>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open, 
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadConAsociadosConIdRef>();
            var entities = entityBuilder.BuildEntities(data);

            Assert.IsNotNull(entities);

            Assert.AreEqual("aaaa", entities[0].Cadena);
            Assert.AreEqual(1, entities[0].Entero);
            Assert.AreEqual("13/05/2009", entities[0].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[0].Entidad);
            Assert.AreEqual("aaaa", entities[0].Entidad.Cadena);

            Assert.AreEqual("bbbb", entities[1].Cadena);
            Assert.AreEqual(2, entities[1].Entero);
            Assert.AreEqual("13/05/2009", entities[1].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[1].Entidad);
            Assert.AreEqual("bbbb", entities[1].Entidad.Cadena);

            Assert.AreEqual("cccc", entities[2].Cadena);
            Assert.AreEqual(3, entities[2].Entero);
            Assert.AreEqual("13/05/2009", entities[2].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[2].Entidad);
            Assert.AreEqual("aaaa", entities[2].Entidad.Cadena);

            Assert.AreEqual("dddd", entities[3].Cadena);
            Assert.AreEqual(4, entities[3].Entero);
            Assert.AreEqual("13/05/2009", entities[3].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[3].Entidad);
            Assert.AreEqual("aaaa", entities[3].Entidad.Cadena);

            Assert.AreEqual("eeee", entities[4].Cadena);
            Assert.AreEqual(5, entities[4].Entero);
            Assert.AreEqual("13/05/2009", entities[4].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[4].Entidad);
            Assert.AreEqual("bbbb", entities[4].Entidad.Cadena);
        }

        [Test]
        public void BuildEntidadConAsociadosTest()
        {
            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadConAsociados>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadConAsociados>();
            var entities = entityBuilder.BuildEntities(data);

            Assert.IsNotNull(entities);

            Assert.AreEqual("aaaa", entities[0].Cadena);
            Assert.AreEqual(1, entities[0].Entero);
            Assert.AreEqual("13/05/2009", entities[0].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[0].Entidad);
            Assert.AreEqual(1, entities[0].Entidad.Id);

            Assert.AreEqual("bbbb", entities[1].Cadena);
            Assert.AreEqual(2, entities[1].Entero);
            Assert.AreEqual("13/05/2009", entities[1].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[1].Entidad);
            Assert.AreEqual(2, entities[1].Entidad.Id);

            Assert.AreEqual("cccc", entities[2].Cadena);
            Assert.AreEqual(3, entities[2].Entero);
            Assert.AreEqual("13/05/2009", entities[2].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[2].Entidad);
            Assert.AreEqual(1, entities[2].Entidad.Id);

            Assert.AreEqual("dddd", entities[3].Cadena);
            Assert.AreEqual(4, entities[3].Entero);
            Assert.AreEqual("13/05/2009", entities[3].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[3].Entidad);
            Assert.AreEqual(1, entities[3].Entidad.Id);

            Assert.AreEqual("eeee", entities[4].Cadena);
            Assert.AreEqual(5, entities[4].Entero);
            Assert.AreEqual("13/05/2009", entities[4].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[4].Entidad);
            Assert.AreEqual(2, entities[4].Entidad.Id);
        }

        [Test]
        public void BuildEntidadConAsociadosYAgregadosTest()
        {
            var factory = new DefaultLayoutFactory();
            var layout =
                factory.Create<EntidadConAsociadosYAgregados>(new LayoutFactoryConfig {GenerateAggregates = true},
                                                              new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadConAsociadosYAgregados>();
            var entities = entityBuilder.BuildEntities(data);

            Assert.IsNotNull(entities);
            Assert.AreEqual(5, entities.Count);

            Assert.AreEqual("aaaa", entities[0].Cadena);
            Assert.AreEqual(1, entities[0].Entero);
            Assert.AreEqual("13/05/2009", entities[0].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[0].Entidad);
            Assert.AreEqual(1, entities[0].Entidad.Id);
            Assert.IsNotNull(entities[0].Agregados);
            Assert.AreEqual(3, entities[0].Agregados.Count);

            Assert.AreEqual("bbbb", entities[1].Cadena);
            Assert.AreEqual(2, entities[1].Entero);
            Assert.AreEqual("13/05/2009", entities[1].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[1].Entidad);
            Assert.AreEqual(2, entities[1].Entidad.Id);
            Assert.IsNull(entities[1].Agregados);

            Assert.AreEqual("cccc", entities[2].Cadena);
            Assert.AreEqual(3, entities[2].Entero);
            Assert.AreEqual("13/05/2009", entities[2].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[2].Entidad);
            Assert.AreEqual(1, entities[2].Entidad.Id);
            Assert.IsNull(entities[2].Agregados);

            Assert.AreEqual("dddd", entities[3].Cadena);
            Assert.AreEqual(4, entities[3].Entero);
            Assert.AreEqual("13/05/2009", entities[3].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[3].Entidad);
            Assert.AreEqual(1, entities[3].Entidad.Id);
            Assert.IsNotNull(entities[3].Agregados);
            Assert.AreEqual(1, entities[3].Agregados.Count);

            Assert.AreEqual("eeee", entities[4].Cadena);
            Assert.AreEqual(5, entities[4].Entero);
            Assert.AreEqual("13/05/2009", entities[4].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[4].Entidad);
            Assert.AreEqual(2, entities[4].Entidad.Id);
            Assert.IsNotNull(entities[4].Agregados);
            Assert.AreEqual(1, entities[4].Agregados.Count);
        }

        [Test]
        public void BuildEntidadConAsociadoTest()
        {
            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadConAsociados>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadConAsociados>();
            var entities = entityBuilder.BuildEntities(data);

            Assert.IsNotNull(entities);
            Assert.AreEqual(12000, entities.Count);

            Assert.AreEqual("aaaa", entities[0].Cadena);
            Assert.AreEqual(1, entities[0].Entero);
            Assert.AreEqual("13/05/2009", entities[0].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[0].Entidad);
            Assert.AreEqual(1, entities[0].Entidad.Id);

            Assert.AreEqual("bbbb", entities[1].Cadena);
            Assert.AreEqual(2, entities[1].Entero);
            Assert.AreEqual("13/05/2009", entities[1].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[1].Entidad);
            Assert.AreEqual(2, entities[1].Entidad.Id);

            Assert.AreEqual("cccc", entities[2].Cadena);
            Assert.AreEqual(3, entities[2].Entero);
            Assert.AreEqual("13/05/2009", entities[2].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[2].Entidad);
            Assert.AreEqual(1, entities[2].Entidad.Id);

            Assert.AreEqual("dddd", entities[3].Cadena);
            Assert.AreEqual(4, entities[3].Entero);
            Assert.AreEqual("13/05/2009", entities[3].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[3].Entidad);
            Assert.AreEqual(1, entities[3].Entidad.Id);

            Assert.AreEqual("eeee", entities[4].Cadena);
            Assert.AreEqual(5, entities[4].Entero);
            Assert.AreEqual("13/05/2009", entities[4].Fecha.ToShortDateString());
            Assert.IsNotNull(entities[4].Entidad);
            Assert.AreEqual(2, entities[4].Entidad.Id);
        }

        [Test]
        public void BuildEntidadSimpleTest()
        {
            var factory = new DefaultLayoutFactory();
            var layout = factory.Create<EntidadSimple>(new LayoutFactoryConfig(), new Type[] {});

            var archivoBuilder = new ExcelLayoutFileReader();
            var data = archivoBuilder.ReadFileData(layout, new FileStream(
                                                               "..\\..\\Domain\\Tools\\Importador\\Archivos\\" +
                                                               layout.Entity.Name + ".xlsx", FileMode.Open,
                                                               FileAccess.ReadWrite, FileShare.Read));

            var entityBuilder = new DefaultEntityBuilder<EntidadSimple>();
            var entities = entityBuilder.BuildEntities(data);

            Assert.IsNotNull(entities);
            Assert.AreEqual(5, entities.Count);

            Assert.AreEqual("aaaa", entities[0].Cadena);
            Assert.AreEqual(1, entities[0].Entero);
            Assert.AreEqual("13/05/2009", entities[0].Fecha.ToShortDateString());

            Assert.AreEqual("bbbb", entities[1].Cadena);
            Assert.AreEqual(2, entities[1].Entero);
            Assert.AreEqual("13/05/2009", entities[1].Fecha.ToShortDateString());

            Assert.AreEqual("cccc", entities[2].Cadena);
            Assert.AreEqual(3, entities[2].Entero);
            Assert.AreEqual("13/05/2009", entities[2].Fecha.ToShortDateString());

            Assert.AreEqual("dddd", entities[3].Cadena);
            Assert.AreEqual(4, entities[3].Entero);
            Assert.AreEqual("13/05/2009", entities[3].Fecha.ToShortDateString());

            Assert.AreEqual(1, entities[4].Id);
            Assert.AreEqual("eeee", entities[4].Cadena);
            Assert.AreEqual(5, entities[4].Entero);
            Assert.AreEqual("13/05/2009", entities[4].Fecha.ToShortDateString());
        }
    }
}